﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using QDP.Platform.ObjService;
using QDP.Platform.Silverlight.Controls;
namespace QDP.Platform.Silverlight
{
    /// <summary>提供对窗口操作的支持</summary>
    public sealed class Tasker
    {
        private static Dictionary<string, PartElement> Parts;
        private static Dictionary<string, string> Store;
        static Tasker()
        {
            Store = new Dictionary<string, string>();
            Parts = new Dictionary<string, PartElement>();
            UIStore = new Dictionary<UserControl, Widget>();
        }
        #region 提供对窗口激活的支持
        private static List<PartElement> Tasks = new List<PartElement>();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Element"></param>
        public static void Active(PartElement Element)
        {
            if (Tasks.Contains(Element) == false)
            {
                var ctrl = Element.WidgetContent as WidgetControl;
                if (ctrl == null)
                {
                    Tasks.Add(Element);
                }
                else
                {
                    if (ctrl.IsMinimized)
                    {
                        ctrl.OnRestore();
                    }
                }
            }
            else
            {
                Tasks.Remove(Element);
                Tasks.Add(Element);
            }
            if (Tasks.Count > 1) // 表示之前有打开的窗口，需要将其Unactive
            {
                Tasks[Tasks.Count - 2].Unactive();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static void ActiveLast()
        {
            Tasks[Tasks.Count - 2].Active();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Element"></param>
        public static void Remove(PartElement Element)
        {
            if (Tasks.Contains(Element) == true)
            {
                Tasks.Remove(Element);
            }
        }
        #endregion //#region 提供对窗口激活的支持

        #region 提供对子窗口的支持
        private static Dictionary<UserControl, Widget> UIStore;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="UI"></param>
        /// <param name="Parent"></param>
        /// <param name="Title"></param>
        /// <param name="Icon"></param>
        /// <param name="Dock"></param>
        public static void ChildShow(UserControl UI
            , UserControl Parent = null
            , string Title = ""
            , string Icon = ""
            , WidgetDock Dock = WidgetDock.Default)
        {
            Widget Contract;
            if (UIStore.ContainsKey(UI))
            {
                // UIStore中存在相同对象，直接读取该组件
                Contract = UIStore[UI];
            }
            else
            {
                // UIStore中不存在该组件，需要创建一个组件
                var Navigation = new NavUISLXap() { Caption = Title, ImageUrl1 = Icon };
                Contract = Q.WidgetManager.CreatePartContract(Navigation, UI);
                UIStore.Add(UI, Contract);
            }

            if (Parent != null)
            {
                double left = Canvas.GetLeft(Parent);
                double top = Canvas.GetTop(Parent);
                double width = Parent.ActualWidth;
                Canvas.SetLeft(Contract.PartElement.WidgetContent, left + width + 10);
                Canvas.SetTop(Contract.PartElement.WidgetContent, top);
            }
            switch (Dock)
            {
                case WidgetDock.Default:
                    {
                        Contract.PartElement.WidgetParent = Q.MainPage.ContentRoot;
                        break;
                    }
                case WidgetDock.Right:
                    {
                        Contract.PartElement.WidgetParent = Q.MainPage.RightRoot;
                        break;
                    }
                case WidgetDock.Left:
                    {
                        Contract.PartElement.WidgetParent = Q.MainPage.LeftRoot;
                        break;
                    }
                case WidgetDock.Top:
                    {
                        Contract.PartElement.WidgetParent = Q.MainPage.TopRoot;
                        break;
                    }
                case WidgetDock.Bottom:
                    {
                        Contract.PartElement.WidgetParent = Q.MainPage.BottomRoot;
                        break;
                    }
                case WidgetDock.Center:
                    {
                        Contract.PartElement.WidgetParent = Q.MainPage.ContentRoot;
                        if (Contract.Configs.ContainsKey("Dock"))
                        {
                            Contract.Configs["Dock"] = "Center";
                            Contract.PartElement.Config["Dock"] = "Center";
                        }
                        else
                        {
                            Contract.Configs.Add("Dock", "Center");
                            Contract.PartElement.Config.Add("Dock","Center");
                        }
                        break;
                    }
            }
            if (Contract == null || Contract.PartElement == null) return;
            if (Contract.PartElement.IsShowed)
            {
                Contract.PartElement.Active();
            }
            else
            {
                Contract.PartElement.Show();
            }
        }
        /// <summary>
        /// 关闭子窗口
        /// </summary>
        /// <param name="UI"></param>
        public static void ChildClose(UserControl UI)
        {
            if (UIStore.ContainsKey(UI))
            {
                if (UIStore[UI].PartElement.IsShowed == true)
                {
                    UIStore[UI].PartElement.Close();
                }
            }
        }
        #endregion //#region 提供对子窗口的支持

        #region 提供对窗口互斥操作的支持
        /// <summary>
        /// 添加应用间的互斥操作。
        /// <para>互斥：即组中的组件打开时，会查看其组中是否含有已经打开的组件，若有则关闭已经打开的组件。</para>
        /// </summary>
        /// <param name="group">互斥的组名称</param>
        /// <param name="key">斥的组件编号：推荐使用CODE或GUID</param>
        /// <param name="element">互斥对象的组件实例</param>
        public static void AddExclusion(string group, string key, PartElement element)
        {
            if (Store.ContainsKey(key) == false)
            {
                Parts.Add(key, element);
                Store.Add(key, group);
            }
        }
        /// <summary>
        /// 触发组件间的互斥效果。
        /// <para>互斥：即组中的组件打开时，会查看其组中是否含有已经打开的组件，若有则关闭已经打开的组件。</para>
        /// </summary>
        /// <param name="key">互斥的组件编号：推荐使用CODE或GUID</param>
        public static void ResumeExclusion(string key)
        {
            try
            {
                if (Store.ContainsKey(key))
                {
                    string groupName = Store[key];
                    var exclusionElement = (from p in Store
                                            where p.Value == groupName && p.Key != key && Parts[p.Key].IsShowed
                                            select Parts[p.Key]).SingleOrDefault();
                    if (exclusionElement == null) return;
                    exclusionElement.Close();
                }
            }
            catch(Exception ex)
            {
                key.ShowMessage(ex.Message,ex.ToString());
            }
        }
        #endregion //#region 提供对窗口互斥操作的支持
    }
}
